package com.achilles.utils;

import android.util.Log;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by AchillesL on 2016-9-21.
 */
public class DateUtils {
    /**
     * 是否是今天
     *
     * @param date
     * @return
     */
    public static boolean isToday(final Date date) {
        return isTheDay(date, DateUtils.now());
    }

    public static boolean isThisWeek(final Date date) {
        Calendar calendarNow = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();

        calendarNow.setTime(DateUtils.now());
        calendar.setTime(date);

        int subYear = calendarNow.get(Calendar.YEAR) - calendar.get(Calendar.YEAR);
        Log.d("isThisWeek","subYear: " + subYear);
        if (subYear != 0) return false;
        int subWeek = calendarNow.get(Calendar.WEEK_OF_YEAR) - calendar.get(Calendar.WEEK_OF_YEAR);
        Log.d("isThisWeek","subWeek: " + subWeek);
        return subWeek == 0;
    }

    public static boolean isThisMonth(final Date date) {
        Calendar calendarNow = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();

        calendarNow.setTime(DateUtils.now());
        calendar.setTime(date);

        int subYear = calendarNow.get(Calendar.YEAR) - calendar.get(Calendar.YEAR);
        if (subYear != 0) return false;
        int subMonth = calendarNow.get(Calendar.MONTH) - calendar.get(Calendar.MONTH);
        return subMonth == 0;
    }

    private static Date now() {
        return new Date();
    }

    /**
     * 是否是指定日期
     *
     * @param date
     * @param day
     * @return
     */
    private static boolean isTheDay(final Date date, final Date day) {
        return date.getTime() >= DateUtils.dayBegin(day).getTime()
                && date.getTime() <= DateUtils.dayEnd(day).getTime();
    }

    /**
     * 获取指定时间的那天 00:00:00.000 的时间
     *
     * @param date
     * @return
     */
    public static Date dayBegin(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取指定时间的那天 23:59:59.999 的时间
     *
     * @param date
     * @return
     */
    public static Date dayEnd(final Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    public static String getTwoDigitNumber(int number) {
        return number < 10 ? "0" + number : number + "";
    }
}
